<?php
namespace App\Models;
use Illuminate\Support\Facades\DB;
use App\Models\ShopStore;

class ShopSeckillGoods extends CommonModel {
	protected $table = 'shop_seckill_goods';

    // 只查询上架的和正常结束
    public function scopeSales($query){
        $query->whereIn('status',[2,3]);
    }
    //正在进行中的
    public function scopeWorking($query){
        $query->where('status','=',2)->where('start_time','<',date('Y-m-d H:i:s'))->where('end_time','>',date('Y-m-d H:i:s'));
    }
    //结束的
    public function scopeEnded($query){
        $query->where('status','=',3);
    }
    //还未开始的
    public function scopeWait($query){
        $query->where('status','=',2)->where('start_time','>',date('Y-m-d H:i:s'));
    }
    public function scopeField($query){
        $query->select('shop_seckill_goods.id','goods_name','goods_image','goods_price','market_price','goods_spec', 'all_stock','stock','status','start_time','end_time')->raw('order by
                (case when goods_status = 2 and shop_seckill_goods.start_time >"'.date('Y-m-d H:i:s').'" and shop_seckill_goods.end_time < "'.date('Y-m-d H:i:s'). '" and shop_seckill_goods.stock>0 then 1
                when goods_status = 2 and shop_seckill_goods.start_time >"'.date('Y-m-d H:i:s').'" and shop_seckill_goods.end_time < "'.date('Y-m-d H:i:s'). '" and shop_seckill_goods.stock=0 then 2
                when goods_status = 2 and shop_seckill_goods.start_time <"'.date('Y-m-d H:i:s').'" then 3
                when goods_status = 2 and shop_seckill_goods.end_time <"'.date('Y-m-d H:i:s').'" then 4
                else 5 end) asc
            ');
    }
    public function scopeList($query){
        $return=$query->pages()
            ->each(function($v,$k){
                $time=date('Y-m-d H:i:s');
                if($v->stock==0){
                    //已抢光
                    $v->time = $v->end_time;
                    $v->status = 3;
                }else if($time<$v->start_time){
                    //未开始
                    $v->time = $v->start_time;
                    $v->status = 1;
                }else if($time>$v->end_time){
                    //已结束
                    $v->time= $v->end_time;
                    $v->status = 0;
                }else if($v->status==3){
                    //已结束
                    $v->time= $v->end_time;
                    $v->status = 0;
                }else{
                    //进行中
                    $v->time = $v->end_time;
                    $v->status = 2;
                }
                unset($v->start_time);
                unset($v->end_time);
            });

        return $return;
    }

    public function _detail(){
        $stock = $this->getAttribute('stock');
        $end_time = $this->getAttribute('end_time');
        $start_time = $this->getAttribute('start_time');
        $status = $this->getAttribute('status');
        $now = date('Y-m-d H:i:s');
        if($stock==0){
            //已抢光
            $time = $end_time;
            $status = 3;
        }else if($now<$start_time){
            //未开始
            $time = $start_time;
            $status = 1;
        }else if($now>$end_time){
            //已结束
            $time= $end_time;
            $status = 0;
        }else if($status==3){
            //已结束
            $time= $end_time;
            $status = 0;
        }else{
            //进行中
            $time = $end_time;
            $status = 2;
        }
        $this->setAttribute('time',$time);
        $this->setAttribute('status',$status);
        $this->setAttribute('time',$time);
    }
    //商品详情
    public function getGoodsDetail($goods_id)
    {

        $goods_detail = DB::table('shop_seckill_goods')->select('id', 'goods_name', 'store_id','start_time','goods_spec', 'store_id','end_time', 'status', 'goods_price', 'market_price', 'goods_image', 'summary', 'all_stock', 'stock')->where(['id' => $goods_id])->first();

        $time = date('Y-m-d H:i:s');
        if ($goods_detail->status == 1) {
            //下架
            $goods_detail->status=0;
            $goods_detail->time = $goods_detail->start_time;
        }else if($goods_detail->status == 3){
            //已抢光
            $goods_detail->time = $goods_detail->end_time;
        }else if($goods_detail->stock==0){
            //已抢光
            $goods_detail->time = $goods_detail->end_time;
            $goods_detail->status = 3;
        }else if($time<$goods_detail->start_time){
            //未开始
            $goods_detail->time = $goods_detail->start_time;
            $goods_detail->status = 1;
        }else if($time>$goods_detail->end_time){
            //已结束
            $goods_detail->time= $goods_detail->end_time;
            $goods_detail->status = 0;
        }else if($goods_detail->status==3){
            //已结束
            $goods_detail->time= $goods_detail->end_time;
            $goods_detail->status = 0;
        }else{
            //进行中
            $goods_detail->time = $goods_detail->end_time;
            $goods_detail->status = 2;
        }
        $store_detail= ShopStore::getStoreDetailById(1);
        $goods_detail->service_id = $store_detail->uid;

        unset($goods_detail->start_time);
        unset($goods_detail->end_time);
        //缓存处理，状态值以数据库为准，商品数量以缓存为准
        $cache_key = 'SeckillGoods_'. $goods_id;
        $data = cache($cache_key);
        if(!$data || $data->status!=$goods_detail->status)
        {
            cache([$cache_key=>$goods_detail],1);	// 缓存1分钟
			
            $data = $goods_detail;
        }
        return $data;
    }

    public function checkGoodsStatus($goods_id,$village_id){

        $check = DB::table('shop_goods_village')->where(['goods_id'=>$goods_id,'village_id'=>$village_id,'goods_type'=>1])->first();
        if(!$check){
            return ['msg'=>'当前小区不支持此商品配送，请切换小区后重试','status'=>0];
        }
        $data =  self::getGoodsDetail($goods_id);
        if($data->status==0){
            return ['msg'=>'商品已下架','status'=>0];
        }
        if($data->status==1){
            return ['msg'=>'秒杀未开始，请重试','status'=>0];
        }
        if($data->stock<=0 || $data->status!=2){
            return ['msg'=>'已被抢光','status'=>0];
        }
        $return = array();
        $store_detail= ShopStore::getStoreDetailById(1);

        unset($data->time);
        unset($data->summary);
        unset($data->all_stock);
        unset($data->stock);
        $data->status = 1;
        $data->goods_number = 1;
        $data->goods_type = 1;
        $store_detail->goods_list[] = $data;
        $store_detail->all_number = 1;                  //商品总数
        $store_detail->all_price = $data->goods_price; //商品总价

        $return['store_list'][] =$store_detail;
        $return['all_price'] = $data->goods_price;
        $return['goods_all_price'] = $data->goods_price;

        return ['status'=>1,'msg'=>'OK','data'=>$return];
    }

    public function buyStatus($goods_id){

        $data = self::getGoodsDetail($goods_id);
        $data->stock -= 1;
        if($data->status==0){
            throw new \Exception ('商品已下架');
        }else if($data->status==1){
            throw new \Exception('秒杀还未开始，请重试。');
        } else if($data->status==3){
            throw new \Exception('您选择的商品已被抢光。');
        }
        if($data->stock<0){
            throw new \Exception('您选择的商品已被抢光。');
        }
        $cache_key = 'SeckillGoods_'.$goods_id;
        cache([$cache_key=>$data],60 * 24 * 1);	// 更新
        return $data;
    }
    //下订单减少库存
    public function updatedGoodsStock($goods_id){
        ShopGoodsStock::addSaleNumber($goods_id,1,1);
        return self::where(['id'=>$goods_id])->decrement('stock');
    }
    //取消订单增加库存
    public function rbackGoodsStock($goods_id){
        ShopGoodsStock::delSaleNumber($goods_id,1,1);
        return self::where(['id'=>$goods_id])->increment('stock');
    }
}